package action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import po.Employment;
import po.Rating;
import po.User;
import service.IEmploymentService;
import service.SearchService;

public class EmploymentAction implements IEmploymentAction{
	private List<Employment> list;
	private IEmploymentService employmentService;

	
	public void setEmploymentService(IEmploymentService employmentService) {
		this.employmentService = employmentService;
	}
	
	public String getEmp() {
		int id;
		HttpServletRequest request = ServletActionContext.getRequest();	
		HttpSession session = request.getSession();
		id = (int) session.getAttribute("userID");
		setList(employmentService.getAllEmployment(id));
		return "success";
	}

	public List<Employment> getList() {
		return list;
	}

	public void setList(List<Employment> list) {
		this.list = list;
	}
	private int empID;
	//申请信息-家教信息
	public void getTutorInfo() {
		HttpServletResponse response = ServletActionContext.getResponse();
		User user = employmentService.searchApplyInfo(empID);
		Rating rat = user.getRat();
		PrintWriter out;
		
		try {
			response.setContentType("text/html");
			response.setCharacterEncoding("UTF-8");
			out = response.getWriter();		
			out.println("账户名："+user.getAccount()+"<br>");
			out.println("联系方式："+user.getUserDetail().getPhone()+"<br>");	
			out.println("电子邮箱："+user.getUserDetail().getEmail()+"<br>");
			
			out.println("完成课程数："+rat.getNumber()+"<br>");
			out.println("用户评价："+"<span style=\"color:green\">"+"好评数："+rat.getGoodMark()+"  </span>"+"<span style=\"color:red\">"+"  差评数:"+rat.getBadMark()+"</span>"+"<br>");
			out.println("综合评分:"+rat.getAvgMark()+"<br>");
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}			
	}

	public int getEmpID() {
		return empID;
	}

	public void setEmpID(int empID) {
		this.empID = empID;
	}
	
	//接受申请
	public String accept() {
		if(employmentService.accept(empID)) {
			return "success";
		}
		return "fail";
	}
	//拒绝申请
	public String refuse() {
		if(employmentService.refuse(empID)) {
			return "success";
		}
		return "fail";
	}
	//确认完成
	public String finish() {
		if(employmentService.finish(empID)) {
			return "success";
		}
		return "fail";
	
	}
	private String money;
	private String date;
	private String address;
	private String text;
	//修改信息
	public String modInfo() {
		if(employmentService.mod(empID, money, date, address, text)) {
			return "success";
		}
		return "fail";
	}
	//删除招聘信息
	public String delete() {
		if(employmentService.delete(empID)) {
			return "success";
		}
		return "fail";
	}

	public String getMoney() {
		return money;
	}

	public void setMoney(String money) {
		this.money = money;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}
}
